//
//  KYZXNewsTableVC.m
//  KuYouZiXun
//
//  Created by lanou on 15/8/5.
//  Copyright (c) 2015年 lanou. All rights reserved.
//
#define kURL @"http://appapi.gamersky.com/v1/contentlist"
#import "KYZXNewsTableVC.h"

#import "LJRequestManagerBlock.h"

#import "KYZXContentListModel.h"

#import "KYZXTableViewCell.h"

#import "KYZXWebVC.h"

@interface KYZXNewsTableVC ()

@end

@implementation KYZXNewsTableVC

#pragma mark 懒加载属性

- (NSMutableArray *)dataSource{

    if(_dataSource == nil){
        _dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
        return _dataSource;
    
}


- (NSDictionary *)parameterDic{

    if (_parameterDic == nil) {
        _parameterDic = @{@"nodes":[self.nodeModel.nodeId stringValue], @"specials":@"" ,@"pageIndex":@"1",@"pageCount":@"20"};
    }
    return _parameterDic;
}


- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"KYZXTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


#pragma mark 获取数据



//请求数据
- (void)requestData:(NSDictionary *)parameterDic{

    NSLog(@"正在获取数据");
    [LJRequestManagerBlock manager:POST requestWith:kURL dic:parameterDic finish:^(NSData *data) {
        [self parseData:data];
    } error:^(NSError *error) {
        NSLog(@"数据获取错误 %@",error);
    }];
}


//解析数据
- (void)parseData:(NSData *)data{

    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    NSArray *resultArray = dict[@"result"];
    for (NSDictionary *dic in resultArray) {
        KYZXContentListModel *model = [[KYZXContentListModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSource addObject:model];
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KYZXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    KYZXContentListModel *model = self.dataSource[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
 self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    return 90;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KYZXContentListModel *model = _dataSource[indexPath.row];
    KYZXWebVC *webVC = [[KYZXWebVC alloc] init];
    webVC.generalId = model.generalId;
    [_newsVC.navigationController pushViewController:webVC animated:YES];
}


@end
